package com.ffcs.web.controllers;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ffcs.web.model.dto.BaogaoResponse;
import com.ffcs.web.model.dto.GetPingGuResponse;
import com.ffcs.web.model.dto.MyReportResponse;
import com.ffcs.web.model.dto.QuestionResponse;
import com.ffcs.web.model.entity.Baogao;
import com.ffcs.web.model.entity.Pinggu;
import com.ffcs.web.model.entity.Question;
import com.ffcs.web.model.entity.WxUser;
import com.ffcs.web.services.PingguService;
import com.ffcs.web.services.WxUserService;

@Controller
public class PingguContrlller {

	private Logger logger = Logger.getLogger(PingguContrlller.class);
	
	@Autowired
	private PingguService pingguService;
	@Autowired
	private WxUserService wxUserService;
	
	/**
	 * 去在线评估页面
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping("gotoPinggu")
	public void gotoPinggu(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		String flag=request.getParameter("flag");
		WxUser wxUser=(WxUser) request.getSession().getAttribute("wxUser");
		wxUser=wxUserService.getUserByUserId(wxUser.getId());
		if(null==wxUser.getTelephone() || "".equals(wxUser.getTelephone())){
			response.sendRedirect("pingguBindjsp.jsp?flag="+flag);
			return ;
		}
		request.getSession().setAttribute("wxUser",wxUser);
		Pinggu pinggu=pingguService.getPingguDesc(flag);
		request.setAttribute("pinggu",pinggu);
		request.setAttribute("flag",flag);
		request.getRequestDispatcher("pinggu/shiPinggu.jsp").forward(request, response);
	}
	
	@RequestMapping("gotoPingguBind")
	public void gotoPingguBind(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		String flag=request.getParameter("flag");
		String telphone=request.getParameter("telphone");
		WxUser wxUser=(WxUser) request.getSession().getAttribute("wxUser");
		wxUserService.bindUser(wxUser, telphone);
		wxUser=wxUserService.getUserByUserId(wxUser.getId());
		request.getSession().setAttribute("wxUser",wxUser);
		Pinggu pinggu=pingguService.getPingguDesc(flag);
		request.setAttribute("pinggu",pinggu);
		request.setAttribute("flag",flag);
		request.getRequestDispatcher("pinggu/shiPinggu.jsp").forward(request, response);
	}
	
	@RequestMapping("tijiaoPinggu")
	public void tijiaoPinggu(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		int pingguId=Integer.parseInt(request.getParameter("pingguId"));
		int score=Integer.parseInt(request.getParameter("score"));
		pingguService.updatePingguDesc(pingguId);
		WxUser wxUser=(WxUser) request.getSession().getAttribute("wxUser");
		Baogao baogao=pingguService.createBaogao(score,pingguId,wxUser);
		response.sendRedirect("pinggu/pingguBaogao.jsp?baogaoId="+baogao.getId());
	}
	
	@RequestMapping("getPingguTiMu")
	@ResponseBody
	public GetPingGuResponse getPingguTiMu(HttpServletRequest request,HttpServletResponse response){
		String pingguId=request.getParameter("pingguId");
		Pinggu pinggu=pingguService.getPingguDesc(pingguId);
		GetPingGuResponse getPingGuResponse=new GetPingGuResponse();
		getPingGuResponse.setResultCode("0");
		getPingGuResponse.setResultDesc("success");
		getPingGuResponse.setPinggu(pinggu);
		return getPingGuResponse;
	}
	
	@RequestMapping("getBaoGao")
	@ResponseBody
	public BaogaoResponse getBaoGao(HttpServletRequest request,HttpServletResponse response){
		String baogaoId=request.getParameter("baogaoId");
		Baogao baogao=pingguService.getBaoGao(baogaoId);
		BaogaoResponse baogaoResponse=new BaogaoResponse();
		baogaoResponse.setResultCode("0");
		baogaoResponse.setResultDesc("success");
		baogaoResponse.setBaogao(baogao);
		return baogaoResponse;
	}
	
	@RequestMapping("getQuestion")
	@ResponseBody
	public QuestionResponse getQuestion(HttpServletRequest request,HttpServletResponse response){
		String questionId=request.getParameter("questionId");
		QuestionResponse questionResponse=new QuestionResponse();
		questionResponse.setResultCode("0");
		questionResponse.setResultDesc("success");
		Question question=pingguService.getQuestion(questionId);
		questionResponse.setQuestion(question);
		return questionResponse;
	}
	
	@RequestMapping("getMyReport")
	@ResponseBody
	public MyReportResponse getMyReport(HttpServletRequest request,HttpServletResponse response){
		WxUser wxUser=(WxUser) request.getSession().getAttribute("wxUser");
		MyReportResponse myReportResponse=pingguService.getMyReport(wxUser);
		return myReportResponse;
	}
}
